﻿using System;
using System.IO;
using ControleRepresentante.Data.Catalogs;
using FluentNHibernate.Cfg.Db;
using NHibernate.Cfg;
using Environment = System.Environment;

namespace ControleRepresentante.UnitTests.Mocks
{
    class MockRepositoryProviderConfigurationSqlExpress : MockRepositoryProviderConfigurationBase
    {
        private static readonly string TempPath = Environment.CurrentDirectory;
        private const string EmptyFilePath = @"Empty.mdf";

        private static int m_Count = 1;

        public MockRepositoryProviderConfigurationSqlExpress(MockSessionConfiguration sessionConfiguration)
            : base(sessionConfiguration)
        {
        }

        public override IPersistenceConfigurer PersistenceConfigurerGenerator(ICatalog catalog)
        {
            string tempFileName = Path.Combine(TempPath, m_Count++ + ".mdf");
            File.Copy(EmptyFilePath, tempFileName, true);
            File.SetAttributes(tempFileName, FileAttributes.Archive | FileAttributes.Normal | FileAttributes.Temporary);
            
            return MsSqlConfiguration.MsSql2008
                .ShowSql()
                .ConnectionString(String.Format(@"
                    Data Source=.\sqlexpress;
                    AttachDbFilename={0};
                    Integrated Security=True;
                    User Instance=True",
                    tempFileName));
        }

        public override void ExposeConfiguration(Configuration configuration)
        {
        }
    }
}